using System;
using System.Windows.Forms;

namespace SharePointUtilitiesForDevelopers.Client.Forms
{
    /// <summary>
    /// A form to display errors.
    /// </summary>
    public partial class ErrorReportForm : Form
    {

        #region Fields

        /// <summary>
        /// The exception.
        /// </summary>
        private Exception _exception;
        
        /// <summary>
        /// The custom message to display.
        /// </summary>
        private string _message;

        /// <summary>
        /// The name of the appliation.
        /// </summary>
        private string _applicationName = "[Application]";

        /// <summary>
        /// The name of the SMTP server or web service url for sending error reports.
        /// </summary>
        private string _errorReportHostName = string.Empty;
        
        /// <summary>
        /// The error report from e-mail address.
        /// </summary>
        private string _errorReportFromAddress = string.Empty;

        /// <summary>
        /// The error report reply to e-mail address.
        /// </summary>
        private string _errorReportReplyToAddress = string.Empty;

        /// <summary>
        /// The list (separated by semi-colons) of email addresses to 
        /// send error info to.
        /// </summary>
        private string _errorReportToAddressList = string.Empty;

        /// <summary>
        /// The list (separated by semi-colons) of email addresses to 
        /// BCC error reports to.
        /// </summary>
        private string _errorReportBccAddressList = string.Empty;

        /// <summary>
        /// The subject line of the error report
        /// </summary>
        private string _errorReportSubject = "Error Report";

        /// <summary>
        /// The error report user name
        /// </summary>
        private string _errorReportUserName = "[Application User]";

        #endregion Fields

        #region Constants

        private const int ExpandHeight = 200;

        #endregion Constants

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the 
        /// <strong>DisplayErrorForm</strong> class.
        /// </summary>
        public ErrorReportForm(
            Exception ex,
            string applicationName):this(
                ex,
                applicationName,
                ex.Message,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty)
        {

        }

        /// <summary>
        /// Initializes a new instance of the 
        /// <strong>DisplayErrorForm</strong> class.
        /// </summary>
        public ErrorReportForm(
            Exception ex,
            string applicationName,
            string message)
            : this(
                ex,
                applicationName,
                message,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty)
        {

        }

        /// <summary>
        /// Initializes a new instance of the 
        /// <strong>DisplayErrorForm</strong> class.
        /// </summary>
        public ErrorReportForm(
            Exception ex,
            string applicationName,
            string message,
            string errorReportHostName,
            string errorReportFromAddress,
            string errorReportToAddressList,
            string errorReportUserName,
            string errorReportSubject)
            : this(
                ex,
                applicationName,
                message,
                errorReportHostName,
                errorReportFromAddress,
                errorReportToAddressList,
                errorReportUserName,
                errorReportSubject,
                string.Empty,
                string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the 
        /// <strong>DisplayErrorForm</strong> class.
        /// </summary>
        /// <remarks>Master constructor</remarks>
        public ErrorReportForm(
            Exception ex,
            string applicationName,
            string message,
            string errorReportHostName,
            string errorReportFromAddress,
            string errorReportToAddressList,
            string errorReportUserName,
            string errorReportSubject,
            string errorReportReplyToAddress,
            string errorReportBccAddressList)
        {
            InitializeComponent();

            // Set fields
            _exception = ex;
            _applicationName = applicationName;
            _message = message;
            _errorReportHostName = errorReportHostName;
            _errorReportFromAddress = errorReportFromAddress;
            _errorReportToAddressList = errorReportToAddressList;
            _errorReportUserName = errorReportUserName;
            _errorReportSubject = errorReportSubject;
            _errorReportReplyToAddress = errorReportReplyToAddress;
            _errorReportBccAddressList = errorReportBccAddressList;
            
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the custom message that appears.
        /// Returns the Exception message if a custom
        /// message is not specified.
        /// </summary>
        public string Message
        {
            get 
            {
                // Return the exception's message by default
                if(_exception != null && String.IsNullOrEmpty(_message))
                    return _exception.Message;
                return _message;
            }
            set { _message = value; }

        }

        /// <summary>
        /// Gets or sets the Exception.
        /// </summary>
        public Exception Exception
        {
            get { return _exception;  }
            set { _exception = value; }

        }

        /// <summary>
        /// Gets or sets the name of the calling application.
        /// </summary>
        public string ApplicationName
        {
            get { return _applicationName; }
            set { _applicationName = value; }

        }

        /// <summary>
        /// Gets or sets the name of the SMTP server or web service url 
        /// for sending error reports.
        /// </summary>
        public string ErrorReportHostName
        {
            get { return _errorReportHostName; }
            set { _errorReportHostName = value; }

        }

        /// <summary>
        /// Gets or sets the from address for sending error reports.
        /// </summary>
        public string ErrorReportFromAddress
        {
            get { return _errorReportFromAddress; }
            set { _errorReportFromAddress = value; }

        }

        /// <summary>
        /// Gets or sets the reply to address for sending error reports.
        /// </summary>
        public string ErrorReportReplyToAddress
        {
            get { return _errorReportReplyToAddress; }
            set { _errorReportReplyToAddress = value; }

        }

        /// <summary>
        /// Gets or sets the list (separated by semi-colons) of email 
        /// addresses to send error reports to.
        /// </summary>
        public string ErrorReportToAddressList
        {
            get { return _errorReportToAddressList; }
            set { _errorReportToAddressList = value; }

        }

        /// <summary>
        /// Gets or sets the list (separated by semi-colons) of email 
        /// addresses to bcc error reports to.
        /// </summary>
        public string ErrorReportBccAddressList
        {
            get { return _errorReportBccAddressList; }
            set { _errorReportBccAddressList = value; }

        }

        /// <summary>
        /// Gets or sets the subject of the error report e-mail.
        /// </summary>
        public string ErrorReportSubject
        {
            get { return _errorReportSubject; }
            set { _errorReportSubject = value; }

        }

        /// <summary>
        /// Gets or sets the name of the application user that is
        /// sending the error report.
        /// </summary>
        public string ErrorReportUserName
        {
            get { return _errorReportUserName; }
            set { _errorReportUserName = value; }

        }

        #endregion Properties

        #region Event Handlers

        /// <summary>
        /// Show/ Hide error details.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showDetailsLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (errorDetailsTextBox.Visible)
            {
                showDetailsLink.Text = "Show Error Details";
                errorDetailsTextBox.Visible = false;
                Size = new System.Drawing.Size(Width, Height - ExpandHeight);
            }
            else
            {
                showDetailsLink.Text = "Hide Error Details";
                errorDetailsTextBox.Visible = true;
                Size = new System.Drawing.Size(Width, Height + ExpandHeight);
            }

        }


        /// <summary>
        /// Form displayed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ErrorReportForm_Shown(object sender, EventArgs e)
        {
            // Format the exception
            errorDetailsTextBox.Text = ErrorReport.FormatErrorReport(
                Exception, 
                Message);
            
            // Set the message
            errorMessageLabel.Text = Message;
            Text = string.Format("{0} Error", ApplicationName);

        }

        /// <summary>
        /// Clipboard click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void copyToClipboardButton_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(errorDetailsTextBox.Text);

        }

        /// <summary>
        /// Handles the Click event of the closeButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion Event Handlers

    }
}